angular.module('BaseControl')

.directive("modalbox", ['$compile','BaseConst', 'BaseEnum', 'BaseModule', 'BaseFunction',
    function($compile, BaseConst, BaseEnum, BaseModule, BaseFunction) {
    return {
        restrict: "E",	// 指令是一个元素 (并非属性)，备选项有"A"、"E" 和 "C"， "M" ,分别代表attribute、element、class和comment（默认值为"A"）
        scope: {
            beforeshow: '&',
            okclick: '&',
            cancelclick: '&'
        },
        templateUrl: 		// 替换HTML (使用scope中的变量)
            'modules/Base/client/views/ModalBox.html',
        replace: true,			// 使用模板替换原始标记
        transclude: true,		// 不复制原始HTML内容
        controller: ['$scope', function($scope) {
            $scope.ID = BaseFunction.getGuid();

            $scope.btnClick = function() {
                if($scope.beforeshow() != false){
                    $('#' + $scope.ID).modal('show');
                }
            };
            $scope.btnOKClick = function(){
                if($scope.okclick() != false){
                    $('#' + $scope.ID).modal('hide');
                }
            };
            $scope.btnCancelClick = function(){
                $('#' + $scope.ID).modal('hide');
                $scope.cancelclick();
            };
        }],
        link: function (scope, element, attrs) {
            scope.Enabled = true;
            scope.Visible = true;
            scope.IsDisplayBtn = true;
            scope.IsDisplayOKBtn = true;
            scope.IsDisplayCancelBtn = true;
            scope.Width = '600px';
            scope.Height = 'auto';
            scope.Text = '按钮';
            scope.WindowTitle = '模态框';

            if (attrs["text"] != undefined && attrs["text"].trim() != "") {
                scope.Text = attrs["text"].trim();
            }
            if (attrs["windowtitle"] != undefined && attrs["windowtitle"].trim() != "") {
                scope.WindowTitle = attrs["windowtitle"].trim();
            }
            if (attrs["width"] != undefined && attrs["width"].trim() != "") {
                scope.Width = attrs["width"].trim();
            }
            if (attrs["height"] != undefined && attrs["height"].trim() != "") {
                scope.Height = attrs["height"].trim();
            }
            if (attrs["isdisplaybtn"] != undefined && attrs["isdisplaybtn"].trim().toLocaleLowerCase() == "false") {
                scope.IsDisplayBtn = false;
            }
            if (attrs["isdisplayokbtn"] != undefined && attrs["isdisplayokbtn"].trim().toLocaleLowerCase() == "false") {
                scope.IsDisplayOKBtn = false;
            }
            if (attrs["isdisplaycancelbtn"] != undefined && attrs["isdisplaycancelbtn"].trim().toLocaleLowerCase() == "false") {
                scope.IsDisplayCancelBtn = false;
            }

            scope.$watch(
                function(){
                    return attrs["enabled"];
                },
                function(){
                    if (attrs["enabled"] != undefined && attrs["enabled"].trim().toLocaleLowerCase() == "false") {
                        scope.Enabled = false;
                    }
                    else{
                        scope.Enabled = true;
                    }
                }
            );

            scope.$watch(
                function(){
                    return attrs["visible"]
                },
                function(){
                    if (attrs["visible"] != undefined && attrs["visible"].trim().toLocaleLowerCase() == "false") {
                        scope.Visible = false;
                    }
                    else{
                        scope.Visible = true;
                    }
                }
            );
        }
    }
}]);
